package com.zhijian.config;

import com.zhijian.interceptor.SysInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Value("${noteImagesFilePath}")
    private String noteImagesFilePath;  // 注入配置的图片路径

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")  // 允许所有源
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);  // 移除allowCredentials，因为和allowedOrigins("*")冲突
    }

    @Bean
    public SysInterceptor sysInterceptor(){
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns = new String[]{
            "/adminLogin",
            "/admin/customerService/login",
            "/product/**",
            "/bigType/**",
            "/users/wxlogin",
            "/weixinpay/**",
            "/admin/note/uploadImage",
            "/image/note/**",     // 具体指定笔记图片路径
            "/image/**/*.jpg",    // 支持各种图片格式
            "/image/**/*.jpeg",
            "/image/**/*.png",
            "/image/**/*.gif"
        };
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/swiper/**").addResourceLocations("file:D:\\java1234-mall\\swiperImgs\\");
        registry.addResourceHandler("/image/bigType/**").addResourceLocations("file:D:\\java1234-mall\\bigTypeImgs\\");
        registry.addResourceHandler("/image/product/**").addResourceLocations("file:D:\\java1234-mall\\productImgs\\");
        registry.addResourceHandler("/image/productSwiperImgs/**").addResourceLocations("file:D:\\java1234-mall\\productSwiperImgs\\");
        registry.addResourceHandler("/image/productIntroImgs/**").addResourceLocations("file:D:\\java1234-mall\\productIntroImgs\\");
        registry.addResourceHandler("/image/productParaImgs/**").addResourceLocations("file:D:\\java1234-mall\\productParaImgs\\");

        // 笔记图片资源映射
        String notePath = noteImagesFilePath;
        if (!notePath.endsWith("/")) {
            notePath = notePath + "/";
        }
        
        System.out.println("Note images path: file:" + notePath);
        
        // 添加多个资源位置以支持不同的路径格式
        registry.addResourceHandler("/image/note/**")
                .addResourceLocations(
                    "file:" + notePath,          // 标准格式
                    "file:///" + notePath        // 绝对路径格式
                )
                .setCachePeriod(3600);
    }



}
